package com.htudata.zabbix.api.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HostZabbixApiService {

  @Autowired
  private BaseZabbixApiService baseZabbixApiService;

  public String createHosts(JSONArray hosts) throws Exception{
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "host.create");
    request.put("params", hosts);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONResult(request);
  }
  
  public String updateHosts(JSONArray hosts) throws Exception {
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "host.update");
    request.put("params", hosts);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONResult(request);
  }
  
  public String deleteHosts(JSONArray hostIds)  throws Exception{
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "host.delete");
    request.put("params", hostIds);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONResult(request);
  }
  
  public String getHosts(JSONObject params) throws Exception {
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "host.get");
    request.put("params", params);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONResult(request);
  }
  
  @Deprecated
  private JSONArray getHostDataByGroupId(String[] output, String[] groupids, String[] selectParentTemplates, String selectItems, String selectTriggers, String selectGraphs, JSONObject search)  throws Exception{
    JSONObject params = new JSONObject();
    params.put("output", output);
    params.put("groupids", groupids);
    params.put("selectGroups", "extend");
    params.put("selectParentTemplates", selectParentTemplates);
    params.put("selectItems", selectItems);
    params.put("selectTriggers", selectTriggers);
    params.put("selectGraphs", selectGraphs);
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "host.get");
    request.put("params", params);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONArrayResult(request);
  }
  
  @Deprecated
  private JSONArray getHostInfoByHostId(String[] output, String[] hostids, String[] selectInterfaces)  throws Exception{
    JSONObject params = new JSONObject();
    params.put("output", output);
    params.put("hostids", hostids);
    params.put("selectInterfaces", selectInterfaces);
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "host.get");
    request.put("params", params);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONArrayResult(request);
  }
  
  @Deprecated
  private JSONArray getHostIdByHostName(String host) throws Exception {
    JSONObject filter = new JSONObject();
    filter.put("host", host);
    JSONObject params = new JSONObject();
    params.put("output", "hostid");
    params.put("filter", filter);
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "host.get");
    request.put("params", params);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONArrayResult(request);
  }
  
  @Deprecated
  private JSONArray getHostIdByIp(String ip) throws Exception {
    JSONObject filter = new JSONObject();
    filter.put("ip", ip);
    JSONObject params = new JSONObject();
    params.put("output", "extend");
    params.put("filter", filter);
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "host.get");
    request.put("params", params);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONArrayResult(request);
  }
}
